<template>
  <div class="address-form-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>添加收货地址</span>
          <el-button
              style="float: right; padding: 3px 0;color: white"
              type="text"
              @click="goBack"
          >
            返回
          </el-button>
        </div>
      </template>

      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="省份" prop="province">
          <el-select v-model="form.province" placeholder="请选择省份" @change="loadCities">
            <el-option v-for="p in provinces" :key="p.code" :label="p.name" :value="p.code" />
          </el-select>
        </el-form-item>

        <el-form-item label="城市" prop="city">
          <el-select v-model="form.city" placeholder="请选择城市" @change="loadDistricts" :disabled="!form.province">
            <el-option v-for="c in cities" :key="c.code" :label="c.name" :value="c.code" />
          </el-select>
        </el-form-item>

        <el-form-item label="区域" prop="district">
          <el-select v-model="form.district" placeholder="请选择区域" :disabled="!form.city">
            <el-option v-for="d in districts" :key="d.code" :label="d.name" :value="d.code" />
          </el-select>
        </el-form-item>

        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入街道门牌号信息" />
        </el-form-item>

        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入11位手机号" maxlength="11" show-word-limit />
        </el-form-item>

        <el-form-item label="收件人" prop="recipient">
          <el-input v-model="form.recipient" placeholder="请输入姓名" />
        </el-form-item>

        <el-form-item label="默认地址" prop="isDefault">
          <el-switch v-model="form.isDefault" active-text="是" inactive-text="否" active-color="#FF7D00" />
        </el-form-item>
      </el-form>

      <div class="button-group">
        <el-button @click="goBack">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';

// 示例数据 - 实际项目中应从API获取
const provinces = ref([
  { code: '110000', name: '北京市' },
  { code: '310000', name: '上海市' },
  { code: '440000', name: '广东省' },
  // 其他省份...
]);

const cities = ref([]);
const districts = ref([]);

const router = useRouter();
const route = useRoute();

const form = reactive({
  province: '',
  city: '',
  district: '',
  address: '',
  phone: '',
  recipient: '',
  isDefault: false
});

const rules = reactive({
  province: [{ required: true, message: '请选择省份' }],
  city: [{ required: true, message: '请选择城市' }],
  district: [{ required: true, message: '请选择区域' }],
  address: [{ required: true, message: '请输入详细地址' }],
  phone: [
    { required: true, message: '请输入手机号' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' }
  ],
  recipient: [{ required: true, message: '请输入收件人' }]
});

const formRef = ref(null);

// 返回上一页
const goBack = () => {
  router.back();
};

// 加载城市列表
const loadCities = (provinceCode) => {
  form.city = '';
  form.district = '';

  // 模拟API请求
  cities.value = {
    '110000': [{ code: '110100', name: '北京市' }],
    '310000': [{ code: '310100', name: '上海市' }],
    '440000': [
      { code: '440100', name: '广州市' },
      { code: '440300', name: '深圳市' }
    ]
  }[provinceCode] || [];

  districts.value = [];
};

// 加载区域列表
const loadDistricts = (cityCode) => {
  form.district = '';

  // 模拟API请求
  districts.value = {
    '440100': [
      { code: '440103', name: '荔湾区' },
      { code: '440104', name: '越秀区' }
    ],
    '440300': [
      { code: '440303', name: '罗湖区' },
      { code: '440304', name: '福田区' }
    ]
  }[cityCode] || [];
};

// 提交表单
const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log('表单数据:', form);

      // 模拟API提交
      setTimeout(() => {
        // 提交成功后返回上一页
        router.back();
      }, 500);
    }
  });
};
</script>

<style scoped>
/* 基础布局 */
.address-form-container {
  padding: 20px;
  max-width: 600px;
  margin: 0 auto;
}

.box-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  margin-top: 20px;
}

/* 橙色主题定制 */
:deep(.el-card__header) {
  background-color: #FF7D00;
  color: white;
  padding: 12px 20px;
}

:deep(.el-button--primary) {
  background-color: #FF7D00;
  border-color: #FF7D00;
}

:deep(.el-button--primary):hover {
  background-color: #FF952B;
  border-color: #FF952B;
}

:deep(.el-switch__core):checked {
  border-color: #FF7D00;
  background-color: #FF7D00;
}
</style>